package com.gmu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmu.domain.ResResult;
import com.gmu.domain.vo.PageVo;
import com.gmu.domain.vo.TblOrderVo;
import com.gmu.mapper.BeSampleUnitsActivitiesMapper;
import com.gmu.pojo.BeSampleUnitsActivities;
import com.gmu.pojo.TblActivities;
import com.gmu.pojo.TblOrder;
import com.gmu.mapper.TblOrderMapper;
import com.gmu.service.TblOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 核酸预约 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@Service
public class TblOrderServiceImpl extends ServiceImpl<TblOrderMapper, TblOrder> implements TblOrderService {

    @Autowired
    private TblOrderMapper tblOrderMapper;

    @Autowired
    private BeSampleUnitsActivitiesMapper beSampleUnitsActivitiesMapper;

    @Override
    public boolean checkIsReservation(Integer userId, Integer activitiesId) {
        TblOrder tblOrder = tblOrderMapper.checkIsReservation(userId,activitiesId);
        return Objects.isNull(tblOrder);
    }

    @Override
    public ResResult<?> getOrderList(Integer userId,Integer status) {
        List<TblOrder> list = tblOrderMapper.getOrderList(userId,status);
        return new ResResult<>(200,list);
    }

    @Override
    public ResResult<?> deleteReservation(Integer id) {
        TblOrder tblOrder = tblOrderMapper.selectById(id);
        Integer beSampleId = tblOrder.getBeSampleId();
        BeSampleUnitsActivities beSampleUnitsActivities = beSampleUnitsActivitiesMapper.selectById(beSampleId);
        Integer orderedPeoples = beSampleUnitsActivities.getOrderedPeoples() - 1;
        beSampleUnitsActivitiesMapper.updatePeoples(beSampleId,orderedPeoples);
        int i = tblOrderMapper.deleteById(id);
        return i!=0 ? new ResResult<>(200,"删除成功"): new ResResult<>(200,"删除失败");
        /*if (tblOrder.getStatus() == 0 || tblOrder.getStatus() == 2) {
            int i = tblOrderMapper.deleteById(id);
            if (i != 0) {
                return new ResResult<>(200,"删除成功");
            }
        } else {
            BeSampleUnitsActivities beSampleUnitsActivities = beSampleUnitsActivitiesMapper.selectById(beSampleId);
            Integer orderedPeoples = beSampleUnitsActivities.getOrderedPeoples() - 1;
            beSampleUnitsActivitiesMapper.updatePeoples(beSampleId,orderedPeoples);
            return new ResResult<>(200,"删除成功");
        }*/
    }

    @Override
    public ResResult<?> getOrderOneList(Integer userId, String idNumber, Integer status) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id",userId);
        map.put("id_number",idNumber);
        QueryWrapper<TblOrder> wrapper = new QueryWrapper<>();
        wrapper.allEq(map);
        TblOrder tblOrder = tblOrderMapper.selectOne(wrapper);
        return new ResResult<>(200,tblOrder);
    }

    @Override
    public PageVo<?> getReservationRecordList(Integer pageNum, Integer pageSize, TblOrderVo tblOrder, String beginTime, String endTime) {
        Page<TblActivities> page = new Page<>(pageNum,pageSize);
        IPage<TblActivities> iPage = tblOrderMapper.getReservationRecordList(page,tblOrder,beginTime,endTime);
        PageVo<TblActivities> pageVo = new PageVo<>();
        pageVo.setCurrent((int) iPage.getCurrent());
        pageVo.setSize((int) iPage.getSize());
        pageVo.setList(iPage.getRecords());
        pageVo.setTotal(iPage.getTotal());
        pageVo.setCode(200);
        return pageVo;
    }

    @Override
    public ResResult<?> deleteReservationRecord(Integer[] ids) {
        int result = tblOrderMapper.deleteReservationRecord(ids);
        return result > 0 ? new ResResult<>(200,"删除成功"):new ResResult<>(200,"删除失败");
    }

    @Override
    public List<TblOrderVo> getOrderVoList(TblOrderVo tblOrder,String beginTime,String endTime) {
        List<TblOrderVo> orderRecordList = tblOrderMapper.getOrderRecordList(tblOrder,beginTime,endTime);
        for (TblOrderVo tblOrderVo:orderRecordList) {
            if (tblOrderVo.getStatus()==0) {
                tblOrderVo.setSta("失败");
            } else if (tblOrderVo.getStatus()==1) {
                tblOrderVo.setSta("待检测");
            } else {
                tblOrderVo.setSta("已检测");
            }
            tblOrderVo.setOrderSta(tblOrderVo.getOrderStatus()==0?"分配":"预约");
        }
        return orderRecordList;
    }
}
